//
//  YHSelectedTableView.m
//  FAKER
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 YHLai. All rights reserved.
//

#import "YHSelectedTableView.h"
#import "AllHeader.h"
#import "FitHeader.h"

#define URLSTRING @"http://dis.myzaker.com/api/get_post_selected.php?_appid=iphone"

@implementation YHSelectedTableView

#pragma mark - 自定义初始化
- (instancetype)initWithFrame:(CGRect)frame
                    URLString:(NSString *)urlString
{
    self = [super initWithFrame:frame];
    if (self) {
        //得到当前页面的urlString
        _currentURLString = urlString;
        
        //获取数据
        _dataSourceArray = [NSMutableArray array];
        [self getDataWithURLString:urlString];
        
        //创建精选tableView
        _tableView = [[BaseClassTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.HEIGHT) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        [self addSubview:_tableView];
        
        //下拉刷新
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self getDataWithURLString:urlString];
        }];
        //上拉加载
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [self getDataWithURLString:_nextURLString];
        }];
    }
    return self;
}

#pragma mark - 获取数据
- (void)getDataWithURLString:(NSString *)urlString
{
    [InternetDataRequest getDataByString:urlString BodyDic:nil WithDataBlock:^(id data) {
        [self.loadingDelegate yhRemoveLoadingView];
        
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
        if ([urlString isEqualToString:_currentURLString]) {
            [_dataSourceArray removeAllObjects];
        }
        NSArray *postsArray = [[(NSDictionary *)data objectForKey:@"data"] objectForKey:@"posts"];
        for (NSDictionary *dict in postsArray) {
            //删除ZAKER相关
            if ([[[dict objectForKey:@"auther"] objectForKey:@"name"] isEqualToString:@"ZAKER懂小姐"] || [[[dict objectForKey:@"auther"] objectForKey:@"name"] isEqualToString:@"话题小秘书"]) {
                continue;
            }
            
            YHSelected *yhSelected = [[YHSelected alloc] init];
            [yhSelected setValuesForKeysWithDictionary:dict];
            [_dataSourceArray addObject:yhSelected];
        }
        [_tableView reloadData];
        
        //上拉加载链接
        _nextURLString = [[[(NSDictionary *)data objectForKey:@"data"] objectForKey:@"info"] objectForKey:@"next_url"];
    }];
}

#pragma mark - tableView
#pragma mark cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSourceArray.count;
}
#pragma mark cell样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"yhSelected";
    YHSelectedTableCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[YHSelectedTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    //cell赋值
    YHSelected *yhSelected = _dataSourceArray[indexPath.row];
    if (yhSelected != nil) {
        cell.contentHeight = [Adaptive AdaptiveHeightByString:yhSelected.content Width:SCREEN_WIDTH - 30 * FITWIDTH FontSize:15];
        cell.yhSelected = yhSelected;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
#pragma mark cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YHSelected *selectedYH = _dataSourceArray[indexPath.row];
    
    //调整content的高度
    CGFloat contentHeight = [Adaptive AdaptiveHeightByString:selectedYH.content Width:SCREEN_WIDTH - 30 * FITWIDTH FontSize:15];
    if (contentHeight > 54.701172) {
        contentHeight = 54.701172;
    }
    
    CGFloat imageHeight = 0;
    switch (selectedYH.medias.count) {
        case 0:
            break;
        case 2:
            imageHeight = (SCREEN_WIDTH - 35 * FITWIDTH) / 2;
            break;
        case 3:
            imageHeight = (SCREEN_WIDTH - 40 * FITWIDTH) / 3;
            break;
            
        case 1:
        default:
            imageHeight = 270 * FITHEIGHT;
            break;
    }
    return 110 * FITHEIGHT + 35 * FITWIDTH + contentHeight + imageHeight;
}
#pragma mark cell点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YHWebViewController *webVC = [[YHWebViewController alloc] init];
    webVC.hidesBottomBarWhenPushed = YES;
    webVC.webURLString = [_dataSourceArray[indexPath.row] content_url];
    webVC.yhSelected = _dataSourceArray[indexPath.row];
//    webVC.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
//    [[UIApplication sharedApplication].delegate.window.rootViewController presentViewController:webVC animated:YES completion:nil];
    
    UITabBarController *tabBarController = (UITabBarController *)[UIApplication sharedApplication].delegate.window.rootViewController;
    [(UINavigationController *)tabBarController.viewControllers[1] pushViewController:webVC animated:YES];
    
//    if ([_currentURLString isEqualToString:URLSTRING]) {
//        [[UIApplication sharedApplication].delegate.window.rootViewController presentViewController:webVC animated:YES completion:nil];
//        [(UINavigationController *)tabBarController.viewControllers[1] pushViewController:webVC animated:YES];
        
//        NSLog(@"%@", [UIApplication sharedApplication].delegate.window.rootViewController);
//    }
//    else {
//        [[UIApplication sharedApplication].delegate.window.rootViewController.presentedViewController presentViewController:webVC animated:YES completion:nil];
//        [(UINavigationController *)tabBarController.viewControllers[1] pushViewController:webVC animated:YES];
        
//        NSLog(@"%@", [UIApplication sharedApplication].delegate.window.rootViewController.presentedViewController);
//    }
}

#pragma mark - 滚动停止
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    _scrollToTopBlock(_tableView.contentOffset.y);
}

#pragma mark - 点击底部刷新按钮
- (void)setStartRefresh:(BOOL)startRefresh
{
    [_tableView.mj_header beginRefreshing];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
